package com.uniledger.exchange;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

/**
 * Activity生命周期监听
 */
public class AppLifeCycleCallback implements Application.ActivityLifecycleCallbacks {

    //维护APP的前后台状态切换
    public static final int STATE_NORMAL = 0;
    public static final int STATE_BACK_TO_FRONT = 1;
    public static final int STATE_FRONT_TO_BACK = 2;


    private int currentAppState = STATE_NORMAL;//当前应用状态
    private BecameListener listener;//
    private UniApp app;//

    private int visibleActivityCount = 0;//可见Activity个数

    public AppLifeCycleCallback(UniApp app, BecameListener listener) {
        this.app = app;
        this.listener = listener;
        this.currentAppState = STATE_NORMAL;
    }

    public int getCurrentAppState() {
        return currentAppState;
    }

    /**
     * Activity 创建完成
     *
     * @param activity
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }


    @Override
    public void onActivityStarted(Activity activity) {

        // 增加可见Activity计数
        visibleActivityCount++;

        // 1限制了从0到1的改变, 后续再开启其他Activity都不会进行状态切换
        if (visibleActivityCount == 1) { // 从后台进入前台
            currentAppState = STATE_BACK_TO_FRONT;
            if (listener != null) {
                listener.onBecameForeground(activity);
            }
        } else {// 否则是正常状态 应用内操作
            currentAppState = STATE_NORMAL;
            if (listener != null) {
                listener.onNormal(activity);
            }
        }

    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    /**
     * 停止
     *
     * @param activity
     */
    @Override
    public void onActivityStopped(Activity activity) {
        // 减少计数
        visibleActivityCount--;

        // 0限制了从有到无的变化
        if (visibleActivityCount == 0) { // 从前台进入后台
            currentAppState = STATE_FRONT_TO_BACK;
            if (listener != null) {
                listener.onBecameBackground(activity);
            }
        } else {
            // 否则是正常状态
            currentAppState = STATE_NORMAL;
            if (listener != null) {
                listener.onNormal(activity);
            }
        }
    }

    /**
     * 应用状态保存
     *
     * @param activity
     * @param outState
     */
    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    /**
     * 销毁
     *
     * @param activity
     */
    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    /**
     * 前后台变换监听
     */
    public interface BecameListener {

        /**
         * 前台操作
         */
        void onBecameForeground(Activity activity);

        /**
         * 后台操作
         *
         * @param activity
         */
        void onBecameBackground(Activity activity);

        /**
         * 应用内正常使用
         */
        void onNormal(Activity activity);

    }

}
